import java.util.Scanner;

class StackByArray {
    private int[] stack; // 堆栈数组
    private int top;     // 栈顶指针

    public StackByArray(int stack_size) {
        stack = new int[stack_size];
        top = -1;
    }

    public boolean push(int data) {
        if (top >= stack.length - 1) {
            System.out.println("堆栈已满,无法再压入");
            return false;
        } else {
            stack[++top] = data;
            return true;
        }
    }

    public boolean empty() {
        return top == -1;
    }

    public int pop() {
        if (empty()) {
            return -1;
        } else {
            return stack[top--];
        }
    }
}

public class DecimalConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("请输入一个十进制数字：");
        int decimal = scanner.nextInt();
        
        System.out.println("请输入想要转换为几进制的数字：");
        int base = scanner.nextInt();
        
        if (base != 2 && base != 8 && base != 16) {
            System.out.println("错误：只支持转换为2、8或16进制");
            return;
        }
        
        convertDecimal(decimal, base);
    }
    
    public static void convertDecimal(int decimal, int base) {
        StackByArray stack = new StackByArray(32); // 假设32位足够
        
        // 处理0的特殊情况
        if (decimal == 0) {
            System.out.println("0");
            return;
        }
        
        // 将余数压入堆栈
        while (decimal > 0) {
            int remainder = decimal % base;
            stack.push(remainder);
            decimal = decimal / base;
        }
        
        // 从堆栈中弹出余数并转换为对应字符
        while (!stack.empty()) {
            int digit = stack.pop();
            if (digit < 10) {
                System.out.print(digit + " ");
            } else {
                // 10->A, 11->B, ..., 15->F
                System.out.print((char)('A' + digit - 10) + " ");
            }
        }
        System.out.println();
    }
}